package com.open.rbac.service.sys.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.open.rbac.model.entity.sys.User;


// Executing an update/delete query
@Transactional(rollbackFor = Exception.class)
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {

    User findByUsername(String username);

    User findByPhone(String phone);

    List<User> findByEmail(String email);

    @Modifying
    @Query("UPDATE user u SET u.enable = :enable WHERE u.id IN (:ids)")
    void updateEnable(List<String> ids, boolean enable);

    @Modifying
    @Query("UPDATE user u SET u.password = ?2 WHERE u.id = ?1")
    void updatePassword(String id, String password);

    @Modifying
    @Query("UPDATE user u SET u.avatar = ?2 WHERE u.id = ?1")
    void updateAvatar(String id, String avatar);

    List<User> findAllByEnable(boolean enable);

}
